package com.zyd.shiro.controller.admin;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.shiro.persistence.beans.UserAddress;
import com.zyd.shiro.business.service.MallUserAddressService;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;

import javax.xml.ws.Response;

/**
 * 控制器层
 * @author RookieWZW
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/address")
public class UserAddressController {

	@Autowired
	private MallUserAddressService userAddressService;
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public ResponseVO findAll(){
		 return ResultUtil.success("查询成功",userAddressService.findAll());
		
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ResponseVO findOne(@PathVariable Integer id){
		return ResultUtil.success("查询成功", userAddressService.findOne(id));	
		
	}
	

	
	/**
	 * 增加
	 * @param userAddress
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ResponseVO add(@RequestBody UserAddress userAddress  ){
		userAddressService.add(userAddress);			
		return ResultUtil.success("增加成功");
	}
	
	/**
	 * 修改
	 * @param userAddress
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public ResponseVO update(@RequestBody UserAddress userAddress,@PathVariable Integer id ){
		userAddress.setId(id);
		userAddressService.update(userAddress);		
		return ResultUtil.success("修改成功");
	}
	


//------------------------------------------------------------------------------------------------------------------------


	/**
	 * 通过userid获取地址列表
	 * @param user_id
	 * @return
	 */
	@RequestMapping(value = "/getList/{id}",method = RequestMethod.GET)
	public ResponseVO getList(@PathVariable Integer id){

		return ResultUtil.success("查询成功",userAddressService.findByUserId(id));
	}

	/**
	 *
	 * @param infoMap
	 * @return
	 */
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public ResponseVO save(@RequestBody Map infoMap){
		userAddressService.save(infoMap);
		return ResultUtil.success("增加成功");
	}


	/***
	 * 收货地址详情
	 * @param user_id
	 * @param address_id
	 * @return
	 */
	@RequestMapping(value = "/detail",method = RequestMethod.GET)
	public ResponseVO detail(Integer user_id,Integer address_id){

		return ResultUtil.success("查询成功",userAddressService.findByUserIdAndAddressId(user_id,address_id));
	}

	/**
	 * 修改地址详情
	 * @param InfoMap
	 * @param userAddress
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}",method = RequestMethod.POST)
	public ResponseVO edit(@RequestBody  Map InfoMap,@PathVariable Integer id){
		userAddressService.edit(InfoMap,id);
		return ResultUtil.success("修改成功");
	}

	/**
	 * 设置默认的收货地址
	 * @param user_id
	 * @param address_id
	 * @return
	 */
	@RequestMapping(value = "/setDefault/{id}",method = RequestMethod.POST)
	public ResponseVO setDefault(Integer user_id,@PathVariable Integer id){
		userAddressService.setDefault(user_id,id);

		return ResultUtil.success("修改成功");
	}


	/**
	 * 删除地址
	 * @param user_id
	 * @param address_id
	 * @return
	 */
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public ResponseVO delete(Integer user_id,Integer address_id){
		userAddressService.delete(user_id,address_id);
		return ResultUtil.success("删除成功");

	}
}
